package com.ccp.dev.qbdms.service;

import com.ccp.dev.core.basic.base.BaseService;
import com.ccp.dev.core.basic.util.UUIDUtils;
import com.ccp.dev.core.basic.util.ContextUtil;
import com.ccp.dev.core.basic.web.query.QueryFilter;
import com.ccp.dev.core.util.BeanUtils;
import com.ccp.dev.core.util.StringUtil;
import com.ccp.dev.core.poi.excel.Excel;
import com.ccp.dev.core.basic.response.ResultData;
import com.ccp.dev.extend.model.ExtendSysFile;
import com.ccp.dev.qbdms.consts.ZhiLiangConst;
import com.ccp.dev.qbdms.dao.WQbdQualityRevSuDao;
import com.ccp.dev.qbdms.model.WQbdQualityRevSu;
import com.ccp.dev.qbdms.model.WQbdQualitymanage;
import com.ccp.dev.qbdms.model.WQbdQualitymanageSu;
import com.ccp.dev.qbdms.util.BaseUtil;
import com.ccp.dev.qbdms.util.ProjectUUIDUtils;
import com.ccp.dev.qbdms.util.ReadAttachmentUtil;
import com.ccp.dev.system.model.SysOrg;
import com.ccp.dev.system.model.SysRole;
import com.ccp.dev.system.util.SysRoleUtil;
import javax.servlet.http.HttpServletRequest;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import com.ccp.dev.qbdms.dao.WQbdQualityReviewDao;
import com.ccp.dev.qbdms.model.WQbdQualityReview;
import net.sf.json.JSONObject;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import javax.annotation.Resource;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;
import org.springframework.util.ObjectUtils;


/**
 * 质量评审管理 信息操作服务类
 *
 * @author prh
 * @date 2020-10-15 10:32:25
 */
@Service
@Transactional(rollbackFor = Exception.class)
public class WQbdQualityReviewService extends BaseService<WQbdQualityReview> {

    @Resource
    private WQbdQualityReviewDao dao;


    @Resource
    private WQbdQualityRevSuDao qbdQualityRevSuDao;
    @Resource
    private CommonalityService commonalityService;

    public HSSFWorkbook exportExcel(Excel excel,QueryFilter queryFilter){
        List<WQbdQualityReview> dataList = this.dao.queryAll(queryFilter);
        int startRow = 1;
        //给每一个单元格赋值
        for (WQbdQualityReview data : dataList) {
        excel.cell(startRow, 0).value(data.getId());
        //excel.cell(startRow, 1).value(data.getMc());
        //可在赋值时，根据需求进行格式化
        //excel.cell(startRow, 2).value(DateFormatUtil.formatDate(data.getRq()));
        //excel.cell(startRow, 3).value(data.getSj());
        //excel.cell(startRow, 4).value(data.getTenantId());
        startRow++;
        }
        return excel.getWorkBook();
    }

    /**
    * 查询上报的业务数据
    *
    * @param
    * @return
    */
    public List<WQbdQualityReview> reportList(String dataId, String tableName) {
        return dao.reportList(dataId, tableName);
    }

    /**
    * 导出用查询
    *
    * @param queryFilter 查询参数
    * @return list
    */
    public List<WQbdQualityReview> queryAll(QueryFilter queryFilter) {
        return dao.queryAll(queryFilter);
    }
















    /**
    * 导入新增
    *
    * @param list 数据集
    * @throws Exception 异常
    */
    public void addImportData(List<WQbdQualityReview> list) {
        ZhiLiangConst.addCommonFields(list);
        //上报状态
        BaseUtil.SystemLevel currentSystemLevel = BaseUtil.getCurrentSystemLevel();
        list.forEach(a -> {
            String mainId = ProjectUUIDUtils.getUUIDFor32();
            a.setId(mainId);
            //上报状态
            if (BaseUtil.SystemLevel.HOSPITALLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.REPORT_STATUS_GROUPTOBEREPORTED);
            } else if (BaseUtil.SystemLevel.PLANTLEVEL.equals(currentSystemLevel)) {
                a.setReportStatus(ZhiLiangConst.getDataInitStatusToHospital());
            }
        });
        if (!list.isEmpty()) {
            dao.addList(list);
        }
    }


    /**
    * 保存接口传输的数据
    *
    * @param wQbdQualityReviewList 主表model
    * @return
    */
    public void saveAll(List<WQbdQualityReview> wQbdQualityReviewList, List<ExtendSysFile> extendSysFileList) {
        ReadAttachmentUtil.getEntityData(wQbdQualityReviewList, extendSysFileList);
            wQbdQualityReviewList.forEach(wQbdQualityReview -> {
            wQbdQualityReview.setBelongdeptname(BaseUtil.getCurrentTenOrgCodePath(wQbdQualityReview.getFilldeptid()));
            if (BeanUtils.isEmpty(dao.getById(wQbdQualityReview.getId()))) {
                dao.add(wQbdQualityReview);
            } else {
                dao.update(wQbdQualityReview);
            }
        });
    }




    @Override
    public int add( WQbdQualityReview wQbdQualityReview) {
        addSubData(wQbdQualityReview);
        return super.add(wQbdQualityReview);
    }
    public int update(WQbdQualityReview wQbdQualityReview,HttpServletRequest request) {
        SysRole currentRole = SysRoleUtil.getCurrentRole(request);
        if (currentRole.getAlias().equals("bpm_xhzly")){
            qbdQualityRevSuDao.delByRefId(wQbdQualityReview.getId());
            addSubData(wQbdQualityReview);
        }else {
            List<WQbdQualityRevSu> revSuList = wQbdQualityReview.getwQbdQualityRevSuList();
            if(!ObjectUtils.isEmpty(revSuList)){
                for (WQbdQualityRevSu wQbdQualityRevSu:revSuList){
                    qbdQualityRevSuDao.update(wQbdQualityRevSu);
                }
            }
        }
        return super.update(wQbdQualityReview);
    }
    private void addSubData(WQbdQualityReview wQbdQualityReview) {
        if (BeanUtils.isNotEmpty(wQbdQualityReview.getwQbdQualityRevSuList())) {
            if (wQbdQualityReview.getwQbdQualityRevSuList().size() > 0) {
                  List<WQbdQualityRevSu> wQbdQualityRevSus   =wQbdQualityReview.getwQbdQualityRevSuList();

                for (WQbdQualityRevSu wQbdQualityRevSu : wQbdQualityRevSus) {
                    wQbdQualityRevSu.setId(ProjectUUIDUtils.getUUIDFor32());
                    wQbdQualityRevSu.setRefid(wQbdQualityReview.getId());
                    ZhiLiangConst.addCommonFields(wQbdQualityRevSu);
                }
                qbdQualityRevSuDao.addList(wQbdQualityReview.getwQbdQualityRevSuList());
            }
        }
    }


    @Override
    public int delByIdArray(String[] ids) {
        qbdQualityRevSuDao.delByRefIds(ids);
        return super.delByIdArray(ids);
    }


}
